package com.anysoft.controller;

import com.anysoft.model.Goods;
import com.anysoft.model.Goods4JSR303;
import com.anysoft.service.GoodsService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.validation.Valid;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    // 得到一个用来记录日志的对象，这样在打印信息的时候能够标记打印的是哪个类的信息
    private static final Log logger = LogFactory.getLog(GoodsController.class);

    @Autowired
    private GoodsService goodsService;

//    @Autowired
//    // @Resource
//    private Validator validator;

    @RequestMapping("/input")
    public String input(Model model){
        // 如果model中没有goods属性，addGoods.jsp会抛出异常
        // 因为表单标签无法找到modelAttribute属性指定的form backing object
        model.addAttribute("goods",new Goods());
        return "addGoods";
    }

//    @RequestMapping("/save")
//    public String save(@ModelAttribute Goods goods, BindingResult result,Model model){
//        validator.validate(goods,result);
//        if(result.hasErrors()){
//            return "addGoods";
//        }
//        goodsService.save(goods);
//        logger.info("添加成功");
//        model.addAttribute("goodsList",goodsService.getGoods());
//        return "goodsList";
//    }

    @RequestMapping("/save4jsr303")
    public String save4jsr303(@Valid @ModelAttribute("goods") Goods4JSR303 goods, BindingResult result, Model model){
        if(result.hasErrors()){
            return "addGoods";
        }
        goodsService.save(goods);
        logger.info("添加成功");
        model.addAttribute("goodsList",goodsService.getGoods4jsr303());
        return "goodsList";
    }
}
